package com.iflytek.utils;

//import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;


/**
 * 日期工具
 *
 * @author ziyuan_deng
 * @date 2019年11月7日 下午3:15:49
 */
public class DateUtils {

	private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);

	private static final String FULL_FORMAT = "yyyy-MM-dd HH:mm:ss";

	private static final String SIMPLE_FORMAT = "yyyy-MM-dd";

	private static final String FORMAT = "yyyyMMddHHmmss";
	// 一天的时间戳
	private static final long DAY_MILLIS = 86400000;

	// 周名称
	private static final String[] WEEK_NAMES = {"日", "一", "二", "三", "四", "五", "六"};

	private static final String[] QUARTERS = {"01-01", "04-01", "07-01", "10-01"};


	// SimpleDateFormat线程安全
	private static ThreadLocal<Map<String, SimpleDateFormat>> sdfMap = new ThreadLocal<Map<String, SimpleDateFormat>>() {

		@Override
		protected Map<String, SimpleDateFormat> initialValue() {
			return new HashMap<String, SimpleDateFormat>();
		}
	};

	private DateUtils() {
		throw new IllegalAccessError("Utility class");
	}

	/**
	 * 将日期转化为yyyy-MM-dd HH:mm:ss的字符串
	 * @param date
	 * @return
	 */
	public static String formatFullStr(Date date) {
		return null == date ? null : getSdf(FULL_FORMAT).format(date);
	}

	/**
	 * 将日期转化为yyyy-MM-dd的字符串
	 * @param date
	 * @return
	 */
	public static String formatSimpleStr(Date date) {
		return null == date ? null : getSdf(SIMPLE_FORMAT).format(date);
	}

	/**
	 * @notes: 转string
	 * @createTime: 2018年8月21日 上午10:00:38
	 *
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String format(Date date, String pattern) {
		return null == date ? null : getSdf(pattern).format(date);
	}

    /**
     * 格式格时间戳
     * @param timestamp 时间戳
     * @param pattern 格式串
     * @return
     */
	public static String format(Long timestamp, String pattern) {
	    if (timestamp == null) {
	        return "";
        }
	    return format(new Date(timestamp), pattern);
    }

	/**
	 * @notes: 转date
	 * @createTime: 2018年8月21日 上午10:01:06
	 *
	 * @param dateStr
	 * @param pattern
	 * @return
	 */
	public static Date parse(String dateStr, String pattern) {
		try {
			return StringUtils.isEmpty(dateStr) ? null : getSdf(pattern).parse(dateStr);
		} catch (ParseException e) {
			logger.debug(e.getMessage(), e);
			return null;
		}
	}

	public static String formatStrToFullFormatStr(String formatTimeStr) {
		if (!StringUtils.hasText(formatTimeStr)) {
			return null;
		}
		SimpleDateFormat formater = new SimpleDateFormat(FORMAT);
		try {
			Date date = formater.parse(formatTimeStr);
			SimpleDateFormat df = new SimpleDateFormat();
			return df.format(date);
		} catch (Exception e) {
			logger.error("时间工具类将yyyyMMddHHmmss格式的时间字符串转换为yyyy-MM-dd HH:mm:ss格式的时间字符串发生异常", e);
			return null;
		}
	}

	private static SimpleDateFormat getSdf(final String pattern) {
		Map<String, SimpleDateFormat> tl = sdfMap.get();
		SimpleDateFormat sdf = tl.get(pattern);
		if (sdf == null) {
			logger.debug(Thread.currentThread().getName() + " put new sdf of pattern " + pattern + " to map");
			sdf = new SimpleDateFormat(pattern);
			tl.put(pattern, sdf);
		}
		return sdf;
	}

	/**
	 * 得到 (Date类型的日期-N个月) 的日期字符串 减法
	 *
	 * @param date
	 * @param month
	 * @return
	 */
	public static Date getSubtractMonth(Date date, int month) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.add(Calendar.MONTH, -month);
		return now.getTime();
	}

	/**
     * 得到几天前的时间
     *
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return now.getTime();
    }

	/**
	 * 得到几天后的日期
	 * @param d
	 * @param day
	 * @return
	 */
	public static Date getDateAfter(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return now.getTime();
	}
	/**
	 * 得到几分钟后的时间
	 * @param minute
	 * @return
	 */
	public static Date getMinuteAfter( int minute) {
		Calendar now = Calendar.getInstance();
		now.add(Calendar.MINUTE, minute);
		return now.getTime();
	}
    /**
     * 得到几天前格式为yyyy-MM-dd的时间
     *
     * @param d
     * @param day
     * @return
     */
    public static String getYMDDateBefore(Date d, int day) {
    	Date beforeDate = getDateBefore(d, day);
    	return format(beforeDate, SIMPLE_FORMAT);
    }

    /**
     * 判断两个时间的大小 是否前面那个小于或等于后面那个 格式 yyyy-MM-dd
     *
     * @param startStr
     * @param endStr
     * @return true为是 false为否
     */
    public static boolean judgeDateEqual(String startStr, String endStr) {
        Date startDate = stringToSimpleDate(startStr);
        Date endDate = stringToSimpleDate(endStr);
        if (startDate.before(endDate) || startDate.compareTo(endDate) == 0) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否是同一天
     *
     * @param startStr
     * @param endStr
     * @return
     */
    public static boolean judgeSameDate(String startStr, String endStr) {
        Date startDate = stringToSimpleDate(startStr);
        Date endDate = stringToSimpleDate(endStr);
        if (startDate.compareTo(endDate) == 0) {
            return true;
        }
        return false;
    }



    /**
     * 字符串日期转Date类型
     *
     * @param str
     * @return
     */
    public static Date stringToSimpleDate(String str) {
        if (!StringUtils.hasText(str)){
			return null;
		}
        try {
        	SimpleDateFormat simPatter = new SimpleDateFormat(SIMPLE_FORMAT);
            return simPatter.parse(str);
        } catch (ParseException e) {
            return null;
        }
    }

	public static Date stringToFullDate(String str) {
		if (!StringUtils.hasText(str)){
			return null;
		}
		try {
			SimpleDateFormat simPatter = new SimpleDateFormat(FULL_FORMAT);
			return simPatter.parse(str);
		} catch (ParseException e) {
			return null;
		}
	}

    /**
     * 将string日期转换为指定格式的日期
     *
     * @param str
     * @param fomart
     * @return
     */
    public static String formatFullStrToFormat(String str, String fomart) {
        try {
        	SimpleDateFormat formtter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            Date date = formtter.parse(str);
            SimpleDateFormat df = new SimpleDateFormat(fomart);
            return df.format(date);
        } catch (Exception e) {
            return "-";
        }
    }

    /**
     * 将时间转换为时间戳
     *
     * @param dateTime
     * @return
     */
    public static long dateToStamp(String dateTime) {
    	try {
            SimpleDateFormat simpleDateFormat = getSdf(FULL_FORMAT);
            Date date = simpleDateFormat.parse(dateTime);
            return date.getTime();
    	} catch (Exception e) {
            return 0;
        }

    }

	/**
	 * 时间戳转日期
	 * @ziyuan_deng
	 * @param dateTime
	 * @return
	 */
	public static String stampToDate(long dateTime) {
    	try {
            SimpleDateFormat simpleDateFormat = getSdf(SIMPLE_FORMAT);
            Date date = new Date(dateTime);
			String day = simpleDateFormat.format(date);
            return day;
    	} catch (Exception e) {
            return null;
        }

    }
	/**
	 * 时间戳转日期
	 * @ziyuan_deng
	 * @param dateTime
	 * @return
	 */
	public static String stampToFullDate(long dateTime) {
    	try {
            SimpleDateFormat simpleDateFormat = getSdf(FULL_FORMAT);
            Date date = new Date(dateTime);
			String day = simpleDateFormat.format(date);
            return day;
    	} catch (Exception e) {
            return null;
        }

    }


    /**
     * 获取日期的月份
     * @ziyuan_deng
     * @param date
     * @return
     */
    public static int getDateMonth(Date date) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		return now.get(Calendar.MONTH) + 1;
	}
    /**
     * 获取日期的天号
     * @ziyuan_deng
     * @param date
     * @return
     */
    public static int getDateDay(Date date){
    	Calendar nowDay = Calendar.getInstance();
		nowDay.setTime(date);
        return nowDay.get(Calendar.DATE);
    }

	/**
	 * 获取日期的年份
	 * @ziyuan_deng
	 * @param date
	 * @return
	 */
	public static Integer getDateYear(Date date) {
    	Calendar now = Calendar.getInstance();
        now.setTime(date);
        return now.get(Calendar.YEAR);
    }

	/**
	 * 根据日期 找到对应日期的 星期
	 * @ziyuan_deng
	 */
	public static String getDayOfWeekByDate(String date) {
		String dayOfweek = "-1";
		try {
			SimpleDateFormat myFormatter = getSdf(SIMPLE_FORMAT);
			Date myDate = myFormatter.parse(date);
			SimpleDateFormat formatter = new SimpleDateFormat("E");
			String str = formatter.format(myDate);
			dayOfweek = str;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return dayOfweek;
	}


	//根据两个日期计算两者的天数
	public static int daysBetween(Date now, Date returnDate)
	{
		Calendar cNow = Calendar.getInstance();
		Calendar cReturnDate = Calendar.getInstance();
		cNow.setTime(now);
		cReturnDate.setTime(returnDate);
		setTimeToMidnight(cNow);
		setTimeToMidnight(cReturnDate);
		long todayMs = cNow.getTimeInMillis();
		long returnMs = cReturnDate.getTimeInMillis();
		long intervalMs = todayMs - returnMs;
		return millisecondsToDays(intervalMs);
	}
	public static void setTimeToMidnight(Calendar calendar) {
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
	}
	public static int millisecondsToDays(long intervalMs) {
		return (int) (intervalMs / (1000 * 86400));
	}

	/**
	 * 获取某个日期所在的周的星期一是什么日期
	 * @ziyuan_deng
	 * @param time
	 * @return
	 */
	public static Date getMonday(Date time) {
		SimpleDateFormat sdf = getSdf(SIMPLE_FORMAT);
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		//判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if(1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		int day = cal.get(Calendar.DAY_OF_WEEK);
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);
		return cal.getTime();
	}

	/**
	 * @ziyuan_deng
	 * @param time
	 * @return
	 */
	public static String getMondayStr(Date time) {
		SimpleDateFormat sdf = getSdf(SIMPLE_FORMAT);
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		//判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if(1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		int day = cal.get(Calendar.DAY_OF_WEEK);
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);
		return sdf.format(cal.getTime());
	}

	/**
	 * 获取某个日期所在的周的星期日是什么日期
	 * @ziyuan_deng
	 * @param time
	 * @return
	 */
	public static String getSundayStr(Date time) {
		SimpleDateFormat sdf = getSdf(SIMPLE_FORMAT);
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		//判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		//获得当前日期是一个星期的第几天
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if(1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		//获得当前日期是一个星期的第几天
		int day = cal.get(Calendar.DAY_OF_WEEK);
		//根据日历的规则
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);
		cal.add(Calendar.DATE, 6);
		return sdf.format(cal.getTime());
	}

	public static  int getIntervalYearCount(Date start,Date end){
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(start);
		c2.setTime(end);
		int count = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
		return count;
	}

	/**
	 * 获取某个日期所在周的星期天
	 * @ziyuan_deng
	 * @param time
	 * @return
	 */
	public static Date getSunday(Date time) {
		SimpleDateFormat sdf = getSdf(SIMPLE_FORMAT);
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		//判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		//获得当前日期是一个星期的第几天
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if(1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		//获得当前日期是一个星期的第几天
		int day = cal.get(Calendar.DAY_OF_WEEK);
		//根据日历的规则
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);
		cal.add(Calendar.DATE, 6);
		//return sdf.format(cal.getTime());
		return  cal.getTime();
	}

	/**
	 * 计算两个日期之间的时间差（以天为单位）
	 * @ziyuan_deng
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static long until(LocalDate startDate, LocalDate endDate){

		return startDate.until(endDate, ChronoUnit.DAYS);

	}

	/**
	 * 计算某一日期与今天的间隔时间差（以天为时间单位）
	 * @ziyuan_deng
	 * @param endDate
	 * @return
	 */
	public static long until(LocalDate endDate){

		return LocalDate.now().until(endDate, ChronoUnit.DAYS);

	}

	/**
	 * 两个日期之间间隔的周数
	 * @ziyuan_deng
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static  long  DateInterval(LocalDate startDate,LocalDate endDate){
        return until(startDate,endDate)/7+1;
	}
	/**
	 * 两个日期之间间隔的周数
	 * @ziyuan_deng
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static  long  DateInterval(Date startDate,Date endDate){
		Instant instant = startDate.toInstant();
		ZoneId zone = ZoneId.systemDefault();
		LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
		LocalDate startTime = localDateTime.toLocalDate();
		Instant instant2 = endDate.toInstant();
		ZoneId zone2 = ZoneId.systemDefault();
		LocalDateTime localDateTime2 = LocalDateTime.ofInstant(instant2, zone2);
		LocalDate endTime = localDateTime2.toLocalDate();

		return until(startTime,endTime)/7+1;
	}

	/**
	 * 获取某月的天数
	 * @ziyuan_deng
	 * @param year
	 * @param month
	 * @return
	 */
	public static Integer daysCount(int year, int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DATE, 0);
		return cal.get(Calendar.DATE);
	}

	/**
	 * 根据日期获取当天是周几
	 * @author ziyuan_deng
	 * @param datetime 日期
	 * @return 周几
	 */
	public static String dateToWeek(String datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		Calendar cal = Calendar.getInstance();
		Date date;
		try {
			date = sdf.parse(datetime);
			cal.setTime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		return weekDays[w];
	}

	/**
	 * 对应的日期是星期几
	 * @param datetime
	 * @return
	 */
	public static String dateToWeek(Date datetime) {
		//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
		Calendar cal = Calendar.getInstance();
		cal.setTime(datetime);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		return weekDays[w];
	}

    /**
     * 返回前几年的总天数
     * @param today
     * @return
     */
	public static int getlastYearDayCount(Date today,int n){
        int year = getDateYear(today);
        int sumDays = 0 ;
		int lastYear = year -1;
        for (int i=1; i<=n; i++){
        	lastYear = year -i;
			if(lastYear%4==0 && lastYear%100!=0){
				sumDays = sumDays + 366;
			}else
			if(lastYear%400==0){
				sumDays = sumDays + 366;
			}else {
				sumDays = sumDays + 365;
			}
		}
        return sumDays;
    }

	/**
	 *
	 * @return
	 */
    public static int getCurrentYearOfDays(Date date){
		int year = getDateYear(date);
		int sumDays = 0;
		if(year%4==0 && year%100!=0){
			sumDays =  366;
		}else
		if(year%400==0){
			sumDays = 366;
		}else {
			sumDays = 365;
		}
		return sumDays;
	}

    /**
     * 通过给定的时间戳，获取当前天开始时间，即0点0分0秒
     * @param millis 时间戳
     * @return
     */
	public static long getDayStartTimestamp(long millis) {
        Calendar cl = Calendar.getInstance();
        cl.setTimeInMillis(millis);
        cl.set(Calendar.HOUR_OF_DAY, 0);
        cl.set(Calendar.MINUTE, 0);
        cl.set(Calendar.SECOND, 0);
        cl.set(Calendar.MILLISECOND, 0);
        return cl.getTimeInMillis();
    }

    /**
     * 通过给定的时间戳，获取当前天开始时间，即23点59分59秒
     * @param millis 时间戳
     * @return
     */
    public static long getDayLastTimestamp(long millis) {
        Calendar cl = Calendar.getInstance();
        cl.setTimeInMillis(millis);
        cl.set(Calendar.HOUR_OF_DAY, 23);
        cl.set(Calendar.MINUTE, 59);
        cl.set(Calendar.SECOND, 59);
        cl.set(Calendar.MILLISECOND, 999);
        return cl.getTimeInMillis();
    }

    /**
     * 获取星期的显示明
     * @param week 星期数值表达式
     * @param prefix 显示前缀
     * @return
     */
    public static String getWeekName(int week, String prefix) {
        if (!StringUtils.hasText(prefix)) {
            prefix = "星期";
        }
        week = Math.max(1, Math.min(week, 7)) - 1;
        return prefix + WEEK_NAMES[week];
    }

    /**
     * 获取星期的显示明
     * @param week 星期数值表达式
     * @return
     */
    public static String getWeekName(int week) {
        return getWeekName(week, "星期");
    }

    /**
     * 获取指定时间在一年中处于第几周
     * @param t 给定的时间戳
     * @return
     */
    public static int getWeekOfYear(long t) {
        Calendar cl = Calendar.getInstance();
        cl.clear();
        cl.setTimeInMillis(t);
        return getWeekOfYear(cl);
    }
    /**
     * 获取指定时间在一年中处于第几周
     * @param t 时间对象
     * @return
     */
    public static int getWeekOfYear(Date t) {
        Calendar cl = Calendar.getInstance();
        cl.clear();
        cl.setTime(t);
        return getWeekOfYear(cl);
    }
    /**
     * 获取指定时间在一年中处于第几周
     * @param t 日期对象
     * @return
     */
    public static int getWeekOfYear(Calendar t) {
        if (t == null) {
            return 0;
        }
        return 0;
    }

    /**
     * 判断两对时间戳是否交叉
     * @param s1 原开始时间戳
     * @param s2 源结束时间戳
     * @param t1 目标开始时间戳
     * @param t2 目标结束时间戳
     * @return 有相交则返回 true
     */
	public static boolean isCross(long s1, long s2, long t1, long t2) {
	    return Math.max(s1, t1) <= Math.min(s2, t2);
    }

    /**
     * 获取指定年度第一周第一天的开始时间戳
     * @param year 年度
     * @return
     */
    public static Calendar getYearFirstWeekStart(int year) {
        Calendar cl = Calendar.getInstance();
        cl.clear();
        cl.setFirstDayOfWeek(Calendar.MONDAY);
        cl.set(year, Calendar.JANUARY, 1);
        int[] weeks = {0, 1, 0, 6, 5, 4, 3, 2};
        int offsetDay = weeks[cl.get(Calendar.DAY_OF_WEEK)];
        cl.add(Calendar.DAY_OF_YEAR, offsetDay);
        return cl;
    }

    /**
     * 获取从指定年度开始，至给定时间戳所经芝的周数
     * @param year 年度
     * @param timestamp 时间戳
     * @return
     */
    public static int getWeekDuration(int year, long timestamp) {
        Calendar cl = getYearFirstWeekStart(year);
        long offset = timestamp - cl.getTimeInMillis();
        double week = offset * 1.0 / (7 * DAY_MILLIS);
        return (int) Math.ceil(week);
    }

    /**
     * 获取给定时间戳的周开始戳
     * @param timestamp 时间戳
     * @return
     */
    public static long getWeekStartTimestamp(long timestamp) {
        Calendar cl = Calendar.getInstance();
        cl.clear();
        cl.setTimeInMillis(timestamp);
        cl.setFirstDayOfWeek(Calendar.MONDAY);
        cl.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cl.set(Calendar.HOUR_OF_DAY, 0);
        cl.set(Calendar.MINUTE, 0);
        cl.set(Calendar.SECOND, 0);
        cl.set(Calendar.MILLISECOND, 0);
        return cl.getTimeInMillis();
    }

    /**
     * 获取指定年度指定周数的开妈时间戳
     * @param year 年度
     * @param weekNum 周数
     * @return
     */
    public static long getWeekStartTimestamp(int year, int weekNum) {
        Calendar cl = getYearFirstWeekStart(year);
        cl.add(Calendar.WEEK_OF_YEAR, weekNum - 1);
        return cl.getTimeInMillis();
    }

	/**
	 * 获取周末  月从0开始
	 *
	 * @param year
	 * @param month
	 * @return
	 */
	public static Set<String> getMonthWekDay(int year, int month) {
		Set<String> dateList=new HashSet<String>();
		SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = new GregorianCalendar(year, month, 1);
		int i = 1;
		while (calendar.get(Calendar.YEAR) < year + 1) {
			calendar.set(Calendar.WEEK_OF_YEAR, i++);
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
			if (calendar.get(Calendar.YEAR) == year) {
				System.out.println("周日："+simdf.format(calendar.getTime()));
				dateList.add(simdf.format(calendar.getTime()));
			}
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
			if (calendar.get(Calendar.YEAR) == year) {
				System.out.println("周六："+simdf.format(calendar.getTime()));
				dateList.add(simdf.format(calendar.getTime()));
			}
		}
		return dateList;
	}

	/**
	 * 根据当前时间获取所属季度
	 * @param date
	 * @return
	 */
   public static Integer getQuarterValue(Date date){

	   int dateMonth = getDateMonth(date);
	   if (dateMonth<4){
	   	  return 1;
	   }else if (dateMonth>=4 && dateMonth<7){
	   	  return 2;
	   }else if (dateMonth>=7 && dateMonth<10){
	   	  return 3;
	   }else{
	   	  return 4;
	   }
   }

	/**
	 * 获取当前所在季度的第一天
	 * @param date
	 * @return
	 */
	public static String getNowQuarter(Date date){
		Integer dateYear = getDateYear(date);
		List<String> quarterList = new ArrayList<>();
		for (String quarter : QUARTERS) {
			quarterList.add(dateYear +"-"+quarter);
		}
		Integer quarterValue = getQuarterValue(date);
		return quarterList.get(quarterValue-1);
	}

	/**
	 * 获取当前日期是星期几<br>
	 *
	 * @param date
	 * @return 当前日期是星期几
	 */
	public static int getWeekOfDate(Date date) {
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0){
			w = 0;
		}
		return w;
	}

	/**
	 * 本系统计算周次
	 * @param date
	 * @return
	 */
	public static long computerWeekNum(Date date){
		Integer dateYear = getDateYear(date);
		Date tempDate = DateUtils.stringToFullDate(dateYear + "-01-01 " + "00:00:01");
		Date endDate = date;
		String toWeek = DateUtils.dateToWeek(tempDate);
		Date startDate = null;
		//一年总周数
		long weekCount = 0;
		//当前日期
		Date  now = new Date();
		if (toWeek.equals("星期一")){
			//两个日期间隔的周数
			startDate = tempDate;
			weekCount = DateUtils.DateInterval(tempDate, endDate);
		}else {
			//不是星期一的情况下，要用下一个星期的星期一作为一年周数计算的起点日期
			Date sunday = DateUtils.getSunday(tempDate);
			startDate = DateUtils.getDateAfter(sunday, 1);
			weekCount = DateUtils.DateInterval(startDate, endDate);

		}
		return weekCount;
	}


	public static void main(String[] args) {
        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		Set<String> monthWekDay = getMonthWekDay(2021, 11);*/
		long count = computerWeekNum(new Date());
		System.out.println(count);

		Date tempDate = DateUtils.stringToFullDate(2022 + "-01-01 " + "00:00:01");
		int i = daysBetween(new Date(),tempDate);
		System.out.println(i);
	}
}
